//jq加载函数
$(function () {
    //发送短信的按钮
    $("#get_code").click(()=>{
        var phone=$("#phone");
        var get_code=$("#get_code");
        var count=120;
        if ($.trim(phone.val()).length===0 || !(/^1[3|4|5|7|8][0-9]{9}$/.test(phone.val()))){
            phone.parent().css("border","2px solid red ");
            phone.focus();
            Notice_8("手机号码不能为空或者格式不正确");
            return;
        }
        phone.parent().css("border","2px solid #DDDDDD ");
        //开始验证手机
        $.ajax({
           url:ctx+"/auth_phone",
            type:"post",
            data:{
               phone : phone.val()
            },
            dataType: 'json',
            success:(data)=>{
               if(data.code!=0){
                   Notice_6("此手机已经注册！");
               }else {
                   //开始发送短信
                   $.ajax({
                       url: ctx+'/sms_code',
                       dataType:'json',
                       type:'post',
                       data:{
                           phone:phone.val(),type:1
                       },
                       success:(data)=>{
                           if (data.code==0){
                               //发送成功
                               Notice_6("短信发送成功！");
                               //使用定时器
                               var inter=window.setInterval(()=>{
                                   if(count===0){
                                       window.clearInterval(inter);
                                       get_code.removeAttr("disabled");
                                       get_code.css("background-color","#ECC3A4");
                                       get_code.html("重试");
                                   }else{
                                       count--;
                                       get_code.attr("disabled","true");
                                       //get_code.unbind('click');
                                       get_code.css("background-color","#94F3A5");
                                       get_code.html(count+"后重试");
                                   }
                               },1000)

                           }else {
                               Notice_6(data.msg);
                           }
                       }

                   })
               }
            }

        });

    });
    //注册开始(点击)
    $("#d_register").click(()=>{
        register();
    });
    //回车事件
    $(document).keydown(function(event){
        if(event.keyCode===13){
            register();
        }
      });
    //注册方法
    function register() {
        var username=$('#username');
        var phone=$("#phone");
        var password=$("#password");
        var code=$("#code");
        //验证数据
        if($.trim(username.val()).length===0){
            Notice_8("用户名不能为空！");
            username.parent().css("border","2px solid red ");
            username.focus();
            return;
        }
        if($.trim(password.val()).length===0){
            username.parent().css("border","2px solid #DDDDDD ");
            password.parent().css("border","2px solid red ");
            password.focus();
            Notice_8("密码不能为空！");
            return;
        }
        if ($.trim(phone.val()).length===0 || !(/^1[3|4|5|7|8][0-9]{9}$/.test(phone.val()))){
            username.parent().css("border","2px solid #DDDDDD ");
            password.parent().css("border","2px solid #DDDDDD ");
            phone.parent().css("border","2px solid red ");
            phone.focus();
            Notice_8("手机号码不能为空或者格式不正确");
            return;
        }

        if($.trim(code.val()).length===0){
            username.parent().css("border","2px solid #DDDDDD ");
            password.parent().css("border","2px solid #DDDDDD ");
            phone.parent().css("border","2px solid #DDDDDD ");
            code.parent().css("border","2px solid red ");
            code.focus();
            Notice_8("验证码不能为空！");
            return;
        }
        username.parent().css("border","2px solid #DDDDDD ");
        password.parent().css("border","2px solid #DDDDDD ");
        phone.parent().css("border","2px solid #DDDDDD ");
        code.parent().css("border","2px solid #DDDDDD ");
        //启动loading组件
        $(".d_body").mLoading("show");//显示loading组件
        //ajax交互数据
        var url=ctx+"/add_user";
        var data={username:username.val(),phone:phone.val(),password:password.val(),code:code.val()};
        $.ajax({
            type : "post",
            url : url,
            data : data,
            dataType: "json",
            success:(data)=>{
                //关闭loading
                $(".d_body").mLoading("hide");//显示loading组件
                if(data.code==0){
                    //注册成功的通知
                    Notice_1("注册成功！5s自动跳转登录页面");
                    window.setTimeout(()=>{
                        //执行跳转函数
                        location.href='/';
                    },5000)
                }else {
                    Notice_3(data.msg);
                }
            }
        })
    }

});